CREATE TYPE tu_type_result AS (
  col_name name,
  col_type name,
  col_typecat "char",
  col_required bool
);

CREATE OR REPLACE FUNCTION tu_get_type_cols
(in_schema_name name, in_type0_name name)
RETURNS SETOF tu_type_result LANGUAGE SQL AS
$$
SELECT attname, typname,  typcategory, attnotnull
  FROM pg_attribute a
  JOIN pg_type t ON atttypid = t.oid
 WHERE attrelid = (SELECT oid FROM pg_class 
                    WHERE relnamespace = (SELECT oid FROM pg_namespace 
                                           WHERE nspname = $1) 
                           and relname = $2) 
       and attnum > 0 and not attisdropped 
order by attnum;
$$;

COMMENT ON FUNCTION tu_get_type_cols
(in_schema_name name, in_type_name name) IS 
$$ 
This function looks up a defined composite type, whether a table or a 
custom user defined type.  It returns a list of the columns expected in the 
order in which they are expected.  This can then be used to loop through
properties of application code objects.
$$;
